package com.itheima.objectDemo;

import java.util.StringJoiner;

public class UserNew implements Cloneable{
    String name;
    String passwd;
    int id;
    int[] data;

    public UserNew() {
    }

    public UserNew(String name, String passwd, int id, int[] data) {
        this.name = name;
        this.passwd = passwd;
        this.id = id;
        this.data = data;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        int[] data = this.data;
        int[] newData = new int[data.length];

        for (int i = 0; i < data.length; i++) {
            newData[i] = data[i];
        }
        UserNew u = (UserNew) super.clone();
        // 因为Object为浅克隆
        u.data = newData;
        return u;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return passwd
     */
    public String getPasswd() {
        return passwd;
    }

    /**
     * 设置
     * @param passwd
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    /**
     * 获取
     * @return id
     */
    public int getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * 获取
     * @return data
     */
    public int[] getData() {
        return data;
    }

    /**
     * 设置
     * @param data
     */
    public void setData(int[] data) {
        this.data = data;
    }

    public String toString() {
        return "User{name = " + name + ", passwd = " + passwd + ", id = " + id + ", data = " + arrToString() + "}";
    }

    private String arrToString(){
        StringJoiner sj = new StringJoiner(",", "[", "]");
        for (int i = 0; i < data.length; i++) {
            sj.add(data[i] + "");
        }
        return sj.toString();
    }
}
